package com.xie.dome.searchservice.service.impl;

import com.xie.dome.searchservice.config.DatabaseContextHolder;
import com.xie.dome.searchservice.service.DataBaseService;
import com.xie.dome.searchservice.service.UserService;
import com.xie.dome.searchservice.util.DataBaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 数据库业务层接口实现类
 *
 * @author xie
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private DataBaseService dataBaseService;

    @Override
    public List<Map<String, Object>> findDataList(String showName) throws Exception {

        List<Map<String, Object>> dataBaseList = dataBaseService.findConfigValue(showName);
        if (dataBaseList == null || dataBaseList.size() == 0) {
            throw new Exception("没有对应的数据库配置(" + showName + "),请确认配置");
        }

        String dataSourceKey = DataBaseUtil.loadDataSource(dataBaseList.get(0));

        // 查询基础库
        DatabaseContextHolder.setDataBaseKey(dataSourceKey);

        // 查询数据库配置
        String sql = "select * from T_USER";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        return list;
    }
}
